<?php
namespace app\admin\controller;


use app\common\model\Company as CompanyModel;
use app\common\model\Enroll as EnrollModel;
use app\admin\tool\Parser;
use think\Request;
use app\common\model\Salary as SalaryModel;


/**
 * 后台管理 商家管理
 * Class CompanyAdmin
 * @package app\admin\controller
 */
class CompanyAdmin extends Base{

    /**
     * 公司数据
     */
    public function index(Request $request){
        $company_name = $request->get('company_name');
        $type         = $request->get('type','-1');
        $mobile       = $request->get('mobile','');

        $where = ['is_delete'=>0];
        if($type != -1){
            $where['type'] = $type;
        }
        if(!empty($company_name)){
            $where['company_name'] = ['like','%'.$company_name.'%'];
        }
        if(!empty($mobile)){
            $where['mobile']       = ['like','%'.$mobile.'%'];
        }

        $this->data['title']  = '公司列表';

        $this->company_type = get_company_type();
        $this->data['column'] = $this->column();
        $this->data['model']  = CompanyModel::where($where)->paginate(10,false,['query'=>$request->get()]);
        $this->data['parser'] = new Parser();
        $this->data['query'] = ['company_name'=>$company_name,'type'=>$type,'mobile'=>$mobile];

        return view('',$this->data);
    }


    /**
     * B端的客户 曾经有报名在B端的
     * @param Request $request
     * @throws
     * @return \think\response\View
     */
    public function cumtomerOfB(Request $request){
        $company_id = $request->param('id');
        $company = CompanyModel::get($company_id);
        $this->data['company'] = $company;

        $this->data['title'] = '<span style="color:red;">'.$company['company_name'].'</span>的客户列表';
        $this->data['column'] = [
             'real_name'=>['name'=>'姓名','fun'=>'text','options'=>''],
             'province_name'=>['name'=>'省','fun'=>'text','options'=>''],
             'city_name'=>['name'=>'市','fun'=>'text','options'=>''],
             'district_name'=>['name'=>'区','fun'=>'text','options'=>''],
             'mobile'=>['name'=>'手机','fun'=>'text','options'=>''],
             'add_time'=>['name'=>'报名时间','fun'=>'datetime','options'=>[]],
        ];
        $this->data['parser'] = new Parser();
        $this->data['model'] = EnrollModel::where(['company_one_id'=>$company_id])->paginate(10);
        return view('',$this->data);
    }


    /**
     * 城市运营商的客户
     * @param Request $request
     * @throws
     * @return \think\response\View
     */
    public function cumtomerOfC(Request $request){
        $company_id = $request->param('id');
        $company = CompanyModel::get($company_id);
        $this->data['company'] = $company;

        $this->data['title'] = '<span style="color:red;">'.$company['company_name'].'</span>的客户列表';
        $this->data['column'] = [
            'real_name'=>['name'=>'姓名','fun'=>'text','options'=>''],
            'province_name'=>['name'=>'省','fun'=>'text','options'=>''],
            'city_name'=>['name'=>'市','fun'=>'text','options'=>''],
            'district_name'=>['name'=>'区','fun'=>'text','options'=>''],
            'mobile'=>['name'=>'手机','fun'=>'text','options'=>''],
            'add_time'=>['name'=>'报名时间','fun'=>'datetime','options'=>[]],
        ];
        $this->data['parser'] = new Parser();
        $this->data['model'] = EnrollModel::where(['company_two_id'=>$company_id])->paginate(10);
        return view('',$this->data);
    }


    /**
     * 城市运营商的商家
     * @param Request $request
     * @throws
     * @return \think\response\View
     */
    public function BofC(Request $request){
        $company_id = $request->param('id');
        $company = CompanyModel::get($company_id);
        $this->data['company'] = $company;

        $this->company_type = get_company_type();
        $this->data['title']  = '<span style="color:red;">'.$company['company_name'].'</span>的客户列表';
        $this->data['column'] = $this->column();
        $this->data['parser'] = new Parser();
        $this->data['model']  = CompanyModel::where('belong_to',$company_id)->paginate(10);

        return view('',$this->data);
    }


    /**
     * 佣金记录
     * @throws
     * @param Request $request
     * @return \think\response\View
     */
    public function money(Request $request){
        $company_id = $request->param('id');
        $salary = SalaryModel::where('company_id',$company_id)->paginate(10);

        $company = CompanyModel::get($company_id);


        $this->data['model'] = $salary;
        $this->data['title'] = '<span style="color:red;">'.$company['company_name'].'</span>的佣金记录';
        $this->data['parser'] = new Parser();
        $this->data['column'] = [
            'money'=>['name'=>'佣金','fun'=>'text','options'=>[]],
            'state'=>['name'=>'状态','fun'=>'translate','options'=>['map'=>['0'=>'未生效','1'=>'已生效']]],
            'add_time'=>['name'=>'计算时间','fun'=>'datetime','options'=>['format'=>'Y-m-d']]
        ];

        return view('',$this->data);
    }




    public function column(){
        return [
            'id' => ['name'=>'ID','fun'=>'text','options'=>''],
            'company_name' => ['name'=>'公司名称', 'fun'=>'text','options'=>''],
            'mobile'       => ['name'=>'手机',     'fun'=>'text','options'=>''],
            'type'         => ['name'=>'类型',     'fun'=>'translateMulti', 'options'=>['map'=>$this->company_type]],
            'balance'      => ['name'=>'账户余额', 'fun'=>'text','options'=>[]],
            'withdraw'      => ['name'=>'累计提现', 'fun'=>'text','options'=>[]],
        ];
    }
}